﻿using System;

namespace Validate.Net
{
    /// <summary>
    /// 非正整数校验
    /// </summary>
    public class ValidateNonpositiveIntAttribute : ValidateRegularAttribute
    {
        private static string regularStr = @"^-[1-9]\d*|0$";

        public ValidateNonpositiveIntAttribute() : base(regularStr)
        { }

        public override bool Validate(object value, out string msg)
        {
            Type t =  value.GetType();
            if (ReferenceEquals(t, typeof(int)) || ReferenceEquals(t, typeof(long)) || ReferenceEquals(t, typeof(double)))
            {
                if (base.Validate(value, out msg))
                    return true;
                else
                {
                    msg = "非正整数校验失败";
                    return false;
                }
            }
            else {
                msg = "类型不匹配，非正整数校验失败";
                return false;
            }
        }
    }
}
